package jsyf.oa.service.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.device.DeviceServiceDao;
import jsyf.oa.entity.device.DeviceServiceEntity;
import jsyf.oa.facade.device.DeviceModelFacade;
import jsyf.oa.facade.device.DeviceServiceFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * Created by y on 2018/4/16.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class ServiceDeviceService implements DeviceServiceFacade {
    @Autowired
    private DeviceServiceDao deviceServiceDao;
    @Override
    public String add(String jsonData) {
        JSONObject JSONObject =JSON.parseObject(jsonData);
        DeviceServiceEntity deviceServiceEntity= JSON.toJavaObject(JSONObject, DeviceServiceEntity.class);
        deviceServiceEntity.setDelFlag(0);
        if(deviceServiceDao.add(deviceServiceEntity)>0){
            return "保存成功";
        }
        return "保存失败";
    }

    @Override
    public Boolean removeById(String jsonData) {
        return deviceServiceDao.removeById(JSON.parseObject(jsonData).getString("id")) > 0;
    }

    @Override
    public JSONObject getById(String jsonData) {
        return deviceServiceDao.getJSONById(JSON.parseObject(jsonData).getString("id"));
    }

    @Override
    public JSONObject getDeviceList(String jsonData) {
        StringBuffer sql= new StringBuffer();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyName = jsonParams.getString("companyName");
        String brandName = jsonParams.getString("brandName");
        String deviceTypeName = jsonParams.getString("deviceTypeName");
        String modelName = jsonParams.getString("modelName");
        String key=jsonParams.getString("key");
        if (!StringUtils.isBlank(companyName)){
            companyName = companyName.trim();
            if(!"提车公司".equals(companyName)){
                sql.append(" AND t_dvc_service.`COMPANY_NAME`like " + "'%" + companyName + "%'");
            }
        }

        if (!StringUtils.isBlank(deviceTypeName)){
            deviceTypeName = deviceTypeName.trim();
            if(!"设备类型".equals(deviceTypeName)){
                sql.append(" AND t_dvc_service.DEVICE_TYPE_NAME= " + "'" + deviceTypeName + "'");
            }
        }
        if (!StringUtils.isBlank(brandName)){
            brandName = brandName.trim();
            if(!"品牌".equals(brandName)){
                sql.append(" AND t_dvc_service.BRAND_NAME= " + "'" + brandName + "'");
            }
        }
        if (!StringUtils.isBlank(modelName)){
            modelName = modelName.trim();
            if(!"型号".equals(modelName)){
                sql.append(" AND t_dvc_service.MODEL_NAME= " + "'" + modelName + "'");
            }
        }
        if (!StringUtils.isBlank(key)){
            key = key.trim();
            sql.append(" AND (SEQUENCE LIKE '%" + key + "%' OR REMARK LIKE '%" + key + "%' OR CUSTOMER_NAME LIKE '%" + key + "%'OR SALED_TYPE LIKE '%" + key + "%'OR t_dvc_service.COMPANY_NAME LIKE '%" + key + "%'OR t_dvc_service.DEVICE_TYPE_NAME LIKE '%" + key + "%'OR t_dvc_service.BRAND_NAME LIKE '%" + key + "%'OR t_dvc_service.MODEL_NAME LIKE '%" + key + "%' ) ");
        }
        String requestPage;
        String pageSize;
        if (jsonParams.containsKey("requestPage") && jsonParams.containsKey("pageSize")) {
            requestPage = jsonParams.getString("requestPage");
            pageSize = jsonParams.getString("pageSize");
        } else {
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
        }
        if (StringUtils.isBlank(requestPage)&&StringUtils.isBlank(pageSize)){
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
        }
        List<JSONObject> records=deviceServiceDao.getJSONListByPage1(sql.toString(),new Object[]{},Integer.valueOf(requestPage),Integer.valueOf(pageSize));
        int recordCount = deviceServiceDao.getPageCount1(sql.toString(),new Object[]{});
        int recordSize = Integer.valueOf(pageSize);
        int pageCount = 0;
        if (recordCount < recordSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / recordSize);
            pageCount = ((int) flag) + 1;
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        JSONObject result = new JSONObject();
        JSONObject objSendData = new JSONObject();
        //子对象
        objSendData.put("recordCount", recordCount);
        objSendData.put("pageCount", pageCount);
        objSendData.put("requestPage", 1);
        result.put("pager", objSendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    @Override
    public List<JSONObject> getByCondition(String companyName,String deviceTypeName,String brandName,String modelName,String key) {
        StringBuffer sql= new StringBuffer();
        if (!StringUtils.isBlank(companyName)){
            companyName = companyName.trim();
            if(!"提车公司".equals(companyName)){
                sql.append(" AND t_dvc_service.`COMPANY_NAME`like " + "'%" + companyName + "%'");
            }
        }

        if (!StringUtils.isBlank(deviceTypeName)){
            deviceTypeName = deviceTypeName.trim();
            if(!"设备类型".equals(deviceTypeName)){
                sql.append(" AND t_dvc_service.DEVICE_TYPE_NAME= " + "'" + deviceTypeName + "'");
            }
        }
        if (!StringUtils.isBlank(brandName)){
            brandName = brandName.trim();
            if(!"品牌".equals(brandName)){
                sql.append(" AND t_dvc_service.BRAND_NAME= " + "'" + brandName + "'");
            }
        }
        if (!StringUtils.isBlank(modelName)){
            modelName = modelName.trim();
            if(!"型号".equals(modelName)){
                sql.append(" AND t_dvc_service.MODEL_NAME= " + "'" + modelName + "'");
            }
        }
        if (!StringUtils.isBlank(key)){
            key = key.trim();
            sql.append(" AND (SEQUENCE LIKE '%" + key + "%' OR REMARK LIKE '%" + key + "%' OR CUSTOMER_NAME LIKE '%" + key + "%'OR SALED_TYPE LIKE '%" + key + "%' ) ");
        }
        String requestPage;
        String pageSize;
        requestPage = "1";
        pageSize = Integer.MAX_VALUE + "";

        if (StringUtils.isBlank(requestPage)&&StringUtils.isBlank(pageSize)){
            requestPage = "1";
            pageSize = Integer.MAX_VALUE + "";
        }
        List<JSONObject> records=deviceServiceDao.getJSONListByPage1(sql.toString(),new Object[]{},Integer.valueOf(requestPage),Integer.valueOf(pageSize));
        return  records;
    }

    @Override
    public Boolean update(String jsonData) {
        JSONObject JSONObject =JSON.parseObject(jsonData);
        DeviceServiceEntity deviceServiceEntity= JSON.toJavaObject(JSONObject, DeviceServiceEntity.class);
        return deviceServiceDao.update(deviceServiceEntity)>0;
    }

    @Override
    public Boolean delById(String jsonData) {
        return deviceServiceDao.delById(JSON.parseObject(jsonData).getString("id")) > 0;
    }
    /*@Override
    public JSONObject findCompanyList() {
        JSONObject obj = new JSONObject();
        List<JSONObject> companies = deviceServiceDao.findCompanyList();
        obj.put("companies", companies);
        return obj;
    }*/
}
